// pages/pingu/pingu.js

const app = getApp()

var api = require('../../js/api.js');

Page({

    /**
     * 页面的初始数据
     */
    data: {
        logo:null,
        users: [],
        usersIndex: 0,
        voui: {
            ur: {},
            uiRet: {},
            uis:[],
        },


    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        var that = this
        that.setData({
            logo: app.globalData.logo,
            voui: JSON.parse(options.voui),

            users: app.globalData.users,
            usersIndex: options.usersIndex
        })

        app.log('==>>voui:')
        app.log(that.data.voui)

        let dtoUi = {}
        dtoUi.ur = that.data.voui.ur
        dtoUi.uis = JSON.stringify(app.globalData.uis)

        app.log('==>>dtoUi')
        app.log(dtoUi)

        if (that.data.voui.ur.urineResultId > 0){
            api.getUiReport(dtoUi).then(res=>{
                app.log('==>>生成报告')
                app.log(res.data.data)
                that.setData({
                    voui:res.data.data,
                })
            })
        }else {
            api.saveUi(dtoUi).then(res =>{
                app.log('==>>保存数据')
                app.log(res.data.data)
                that.setData({
                    voui:res.data.data,
                })
            })
        }



        // that.action_data()
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    },
    action_data() {
        app.log('==>>action_data')
        var that = this
        for (let i in that.data.ur) {
            let name = i.replace("result", "").toUpperCase()
            for (let j in that.data.uis) {
                if (name == that.data.uis[j].nameEng) {
                    if (name == 'ACR') {
                        that.data.uis[14].nameValue = that.data.ur[i]
                        if (that.data.ur[i] < 3.4) {
                            that.data.uis[14].pointIndex = 0
                            that.data.uis[14].state = 'safe'
                        } else if (that.data.ur[i] >= 3.4 && that.data.ur[i] <= 33.9) {
                            that.data.uis[14].pointIndex = 1
                            that.data.uis[14].state = 'danger'
                        } else {
                            that.data.uis[14].pointIndex = 3
                            that.data.uis[14].state = 'danger'
                        }
                    } else {
                        for (let k = 0; k < that.data.uis[j].labelArray.length; k++) {
                            if (
                                (that.data.ur[i].indexOf("(") > -1 && that.data.ur[i].indexOf(that.data.uis[j].labelArray[k]) > -1)
                                || that.data.ur[i] == that.data.uis[j].limitArray[k]) {
                                that.data.uis[j].nameValue = that.data.ur[i]
                                that.data.uis[j].pointIndex = k
                                that.data.uis[j].state = that.data.uis[j].resultArray[k]
                                break
                            }

                        }
                    }


                }
            }
        }

        app.log('==>>結果：')
        app.log(that.data.uis)
        that.setData({
            uis: that.data.uis
        })

        that.pro_port()
    },
    pro_port: function () {

        var that = this
        for (let i in that.data.uis) {
            if (that.data.ui_ret.kid.includes(that.data.uis[i].nameEng)) {
                if (that.data.uis[i].state == 'danger') {
                    that.setData({
                        'ui_ret.ret_kid': false
                    })
                }
            }
            if (that.data.ui_ret.liv.includes(that.data.uis[i].nameEng)) {
                if (that.data.uis[i].state == 'danger') {
                    that.setData({
                        'ui_ret.ret_liv': false
                    })
                }
            }
            if (that.data.ui_ret.end.includes(that.data.uis[i].nameEng)) {
                if (that.data.uis[i].state == 'danger') {
                    that.setData({
                        'ui_ret.ret_end': false
                    })
                }
            }
            if (that.data.ui_ret.inn.includes(that.data.uis[i].nameEng)) {
                if (that.data.uis[i].state == 'danger') {
                    that.setData({
                        'ui_ret.ret_inn': false
                    })
                }
            }
            if (that.data.ui_ret.sg.includes(that.data.uis[i].nameEng)) {
                if (that.data.uis[i].state == 'danger') {
                    that.setData({
                        'ui_ret.ret_sg': false
                    })
                }
            }
            if (that.data.ui_ret.ph.includes(that.data.uis[i].nameEng)) {
                if (that.data.uis[i].state == 'danger') {
                    that.setData({
                        'ui_ret.ret_ph': false
                    })
                }
            }

        }

        if (
            !that.data.ui_ret.ret_kid ||
            !that.data.ui_ret.ret_liv ||
            !that.data.ui_ret.ret_end ||
            !that.data.ui_ret.ret_inn ||
            !that.data.ui_ret.ret_sg ||
            !that.data.ui_ret.ret_ph
        ) {
            that.setData({
                'ui_ret.ret': false
            })
        }

        app.log('====>>健康报告：')
        app.log(that.data.ui_ret)
    },
    toShow: function () {

        var that = this
        wx.navigateTo({
            url: '/pages/jilu/shows/shows?voui=' + JSON.stringify(that.data.voui)
        })
    }
})